package PageTable;

import PageClassTable.AddPageClass;
import page.Page;
import page.PageClass;
import user.User;
import utils.JdbcUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class AddPage {


    static Font font3=new Font("宋体",Font.BOLD,20);
    public static void add(int power,String name){
        JFrame frame =new JFrame("添加信息");
        //设置窗体大小
        frame.setSize(500, 400);
        //设置窗体居中
        frame.setLocationRelativeTo(null);
        //取消布局
        frame.setLayout(null);
        //设置窗口不能改变大小
        frame.setResizable(false);
        //按钮盒子
        Box btnbox=Box.createHorizontalBox();
        //提交修改按钮
        JButton subBtn=new JButton("提交");
        //取消修改按钮
        JButton noBtn=new JButton("返回");
        btnbox.add(subBtn);
        btnbox.add(Box.createHorizontalStrut(60));
        btnbox.add(noBtn);
        //定位盒子
        btnbox.setBounds(144,304,500,20);
        frame.add(btnbox);
        //设置窗体是否可见
        frame.setVisible(true);
        //文章名:
        Box namebox=Box.createHorizontalBox();
        JLabel namelabel=new JLabel("文章名:");
        JTextField nameField=new JTextField(15);
        namelabel.setFont(font3);
        nameField.setFont(font3);
        namebox.add(namelabel);
        namebox.add(Box.createVerticalStrut(20));
        namebox.add(nameField);
        namebox.setBounds(100,40,300,30);
        frame.add(namebox);
        //文章类名:
//        JComboBox<String> c1 = new JComboBox<String>();//创建一个下拉列表框c1
        Box classbox=Box.createHorizontalBox();
        JLabel classlabel=new JLabel("分类名:");
        JComboBox<String> classField= new JComboBox<>();
        List<Map<String, Object>> datas= JdbcUtils.JdbcQuery("SELECT page_class_name FROM page_class");
        for(Map<String,Object> map : datas){
            for(String key : map.keySet()) {
                classField.addItem((String) map.get(key));
            }
        }
        classField.addItem("添加新类");
        classlabel.setFont(font3);
        classField.setFont(font3);
        classbox.add(classlabel);
        classbox.add(Box.createVerticalStrut(20));
        classbox.add(classField);
        classbox.setBounds(100,90,300,30);
        frame.add(classbox);
        //文章内容:
        Box textbox=Box.createHorizontalBox();
        JLabel textlabel=new JLabel("内  容:");
        JTextArea textField=new JTextArea(2,30);
        textField.setLineWrap(true);// 激活自动换行功能
        textField.setFont(font3);
        JScrollPane croll = new JScrollPane(textField);
        textlabel.setFont(font3);
        textbox.add(textlabel);
        textbox.add(Box.createVerticalStrut(20));
        textbox.add(croll);
        textbox.setBounds(100,140,300,140);
        frame.add(textbox);
        //按钮逻辑
        noBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
            }
        });
        subBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name_page =nameField.getText().trim();
                String class_page= Objects.requireNonNull(classField.getSelectedItem()).toString();
                String text_page=textField.getText().trim();
                if(Objects.equals(class_page, "添加新类")){
                    class_page=AddPageClass.init(power);
                }
                int yn=JOptionPane.showConfirmDialog(frame, "确认添加该文章？", "确认框", JOptionPane.OK_CANCEL_OPTION);
                if(yn==JOptionPane.YES_OPTION){
                    if(Page.sameName(name_page)){
                        JOptionPane.showMessageDialog(frame, "文章名重复");
                    }else{
                        LocalDateTime time=LocalDateTime.now();
                        JdbcUtils.JdbcUpdate(Page.sql_add, Page.sum+1,name_page,class_page,text_page,time,time,power,name);
                        Page.jssum();
                        JOptionPane.showMessageDialog(frame, "创建成功");
                    }
                }
                frame.dispose();
            }
        });
    }
}
